package com.jrx.anytxn.atom.service.impl;

import com.jrx.anytxn.atom.bean.CustBatchSaveBean;
import com.jrx.anytxn.atom.service.BaseBatchAccoutHandle;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.investor.bean.InvestorCastAccountBean;
import com.jrx.anytxn.investor.entity.AmAccountMainInvestorInfo;
import com.jrx.anytxn.investor.entity.AmLoanAccountInvestorInfo;
import com.jrx.anytxn.investor.entity.AmLoanMainInvestorInfo;
import com.jrx.anytxn.investor.entity.AmRepaymentPlanInvestorInfo;
import com.jrx.anytxn.investor.service.IInvestorAccountService;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @program: anytxn-cf-parent
 * @description: 联合贷批量-抛帐处理
 * @author: xiaoyu.jing
 * @date: 2020-08-31 15:32
 **/
@Order(110)
@Service
public class InvestorCastAccountHandle extends BaseBatchAccoutHandle<InvestorCastAccountBean> {

    Logger logger = LoggerFactory.getLogger(InvestorCastAccountHandle.class);

    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private IInvestorAccountService accountService;

    /**
     * 抛帐业务逻辑处理
     *
     * @return
     * @throws TxnException
     */
    @Override
    public List<InvestorCastAccountBean> handBatchAccout() throws TxnException {
        CustBatchSaveBean custBatchSaveBean = CustBatchHelper.getCustBatchLocal();
        List<AmLoanAccountInvestorInfo> loanAcctList = custBatchSaveBean.getDealInvestorLoanAccountList();
        if (CollectionUtils.isEmpty(loanAcctList)) {
            return null;
        }

        PrOrganizationTable organization = organizationTableService.findByOrId(loanAcctList.get(0).getOrganizationId(), loanAcctList.get(0).getTenantId());
        //业务日期
        Date businessDate = organization.getToday();

        List<AmLoanMainInvestorInfo> dealLoanMainList = custBatchSaveBean.getDealInvestorLoanMainList();
        List<AmRepaymentPlanInvestorInfo> dealPlanList = custBatchSaveBean.getDealInvestorPlanList();
        if(CollectionUtils.isEmpty(dealPlanList)){
            return null;
        }

        List<AmAccountMainInvestorInfo> dealAcctList = custBatchSaveBean.getDealInvestorAcctList();
        List<InvestorCastAccountBean> castAccountBeanList = new ArrayList<>();
        for (AmLoanAccountInvestorInfo amLoanAccountInfo : loanAcctList) {
            String investorLoanId = amLoanAccountInfo.getInvestorLoanId();
            //添加核销订单的判断 如果是核销的订单不做处理
            if (TransBizConstant.YES_FLAG.equals(amLoanAccountInfo.getChargeoffFlag())) {
                logger.info("订单已经核销无需处理。订单号：{}", investorLoanId);
                continue;
            }

            List<AmAccountMainInvestorInfo> castAcctList = dealAcctList.stream().filter(acct -> investorLoanId.equals(acct.getInvestorLoanId()))
                    .filter(acct -> TransBizConstant.ACCOUNT_TYPE_06.equals(acct.getAccountType()))
                    .filter(acct -> businessDate.equals(acct.getPostingDate())).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(castAcctList)){
                continue;
            }

            List<AmRepaymentPlanInvestorInfo> planList = dealPlanList.stream().filter(plan -> investorLoanId.equals(plan.getInvestorLoanId())).collect(Collectors.toList());
            AmLoanMainInvestorInfo amLoanMainInfo = dealLoanMainList.stream().filter(loanMain -> investorLoanId.equals(loanMain.getInvestorLoanId())).findFirst().get();
            InvestorCastAccountBean castAccountBean = accountService.castTxnAccount(amLoanMainInfo, amLoanAccountInfo, planList, businessDate, true);
            if (null != castAccountBean) {
                castAccountBeanList.add(castAccountBean);
            }
        }
        return castAccountBeanList;
    }
}
